// package-common/pages/share-brand/index.js
const app = getApp();
import { COMMON_PAGE, PACKAGE_SIGNIN_PAGE, PACKAGE_STAR_PAGE } from '../../../constants/route';
import { ACTIVITY_TYPE, TARGET_TYPE } from '../../../constants/common';
import { ACTIVITY_MENU_ROUTE } from '../../../constants/activity';
import { ShareImageMixins, ActivityMixins, PageOssMixins, PageCommonFuncs } from '../../../behaviors/page';
import { navTo, showLoading, hideLoading } from '../../../common/env';
import { getDateTS, arr2Obj } from '../../../common/utils';
import { getShareBrandInfo, getShareBrandActivityList } from '../../../api/common';
import { getMangeTargetId, getMangeTargetType } from '../../../biztools/wex';
let loaded = false, brandId;

Page({
    mixins: [ShareImageMixins, ActivityMixins, PageOssMixins, PageCommonFuncs],
    data: {
        ACTIVITY_TYPE,
        TARGET_TYPE,
        pageData: {
            refresh: false,
            groups: [],
        },
        filter: {
            begin: getDateTS(undefined),
            end: getDateTS(undefined, true),
        },
        brandInfo: {},
        activityAsFeatureAvailablesMap: {},
        activityCreateSelectPopup: {
            show: false,
        },
        canManage: false, // 能否管理
    },
    onLoad(options) {
        brandId = options.brandId;
        if (getMangeTargetType() === TARGET_TYPE.brand && getMangeTargetId() === brandId && options.owner) {
            this.setData({
                canManage: true,
            });
        }
    },
    onShow() {
        if (!app.globalData.isReadyAuth) {
            const _ = this;
            const originFunc = app.userIdentifyCallback;
            app.userIdentifyCallback = function (...args) {
                _.onShow(...args);
                return originFunc && originFunc(...args);
            }
            return;
        } else {
            this._handleGetBrandInfo();
            this._handleGetList();
        }
    },
    async _handleGetBrandInfo() {
        let brandInfo = await getShareBrandInfo(brandId);
        brandInfo.brandId = brandId;
        const activityAsFeatureAvailablesMap = arr2Obj(brandInfo.activityAsFeatureAvailables, 'activityType');
        this.setData({ brandInfo, activityAsFeatureAvailablesMap });
    },
    async _handleGetList() {
        !loaded && showLoading('加载中...');
        const { pageData, filter } = this.data;
        const { begin: dateBegin, end: dateEnd } = filter;
        const groups = await getShareBrandActivityList({ brandId, pageNo: 1, ts: Date.now(), dateBegin, dateEnd });
        this.setData({
            pageData: {
                ...pageData,
                refresh: false,
                groups,
            },
        });
        if (!loaded) hideLoading(); loaded = true;
    },
    handlePageRefresh() {
        loaded = false;
        this.setData({ 'pageData.refresh': true });
        this._handleGetList();
    },
    handleFilter(e) {
        const { begin, end } = e.detail;
        this.setData({
            filter: { begin, end },
        }, () => {
            this._handleGetList();
        });
    },
    handleNavToApplySpokesman() {
        navTo(COMMON_PAGE['shareApplySpokesman'], { brandId });
    },
    handleNavToApplyDistributor() {
        navTo(COMMON_PAGE['shareApplyDistributor'], { brandId });
    },
    handleNavToSignin() {
        navTo(PACKAGE_SIGNIN_PAGE['detail'], { brandId });
    },
    handleNavToEdit() {
        if (!this.data.canManage) return;
        navTo(PACKAGE_STAR_PAGE['brandSetting']);
    },
    handleShowActivityCreateSelect() {
        this.setData({
            'activityCreateSelectPopup.show': true,
        });
    },
    handleActivityCreateSelect(e) {
        const type = e.detail;
        let pageUrl = ACTIVITY_MENU_ROUTE[TARGET_TYPE.brand][type];
        navTo(pageUrl);
    },
})
